<?php

namespace api\modules\mini\forms\user;

use common\models\user\UserWithdrawAccount;
use Yii;
use yii\base\Model;

/**
 * Class UserWithdrawAccountForm
 * @package api\modules\mini\forms\user
 */
class UserWithdrawAccountForm extends Model
{
    /**
     * @var string $name 姓名
     * @var string $mobile 手机号
     * @var string $referrer_name 推荐人
     */
    public $id;
    public $type;
    public $card_no;
    public $name;
    public $bank_name;

    /**
     * @var UserWithdrawAccount $_userWithdrawAccount
     */
    protected $_userWithdrawAccount;


    public function rules()
    {
        return [
            // 参数验证
            [['type', 'card_no', 'name'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],

            [['type'], 'checkType', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['card_no'], 'checkCardNo', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['name'], 'checkName', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['bank_name'], 'checkBankName', 'skipOnEmpty' => false, 'on' => ['edit']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['edit'] = ['type', 'card_no', 'name', 'bank_name'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'type' => '提现方式',
            'card_no' => '账号',
            'name' => '姓名',
            'bank_name' => '银行卡',
        ];
    }

    /**
     * 检验姓名
     * @param $attribute
     * @return void
     */
    public function checkType($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }
        $types = UserWithdrawAccount::$types;
        if (isset($types[$this->type]) === false) {
            $this->addError($attribute, '提现方式异常');
            return;
        }
    }

    /**
     * 检验账号
     * @param $attribute
     * @return void
     */
    public function checkCardNo($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }
        if (mb_strlen($this->card_no) > 32) {
            $this->addError($attribute, '账号长度超出[1-32位]');
            return;
        }
    }

    /**
     * 检验姓名
     * @param $attribute
     * @return void
     */
    public function checkName($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }

        if (mb_strlen($this->name) > 32) {
            $this->addError($attribute, '姓名过长[1-32位]');
            return;
        }
    }

    /**
     * 检验银行
     * @param $attribute
     * @return void
     */
    public function checkBankName($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->type == UserWithdrawAccount::TYPE_ALIPAY) {
            return;
        }
        if (mb_strlen($this->bank_name) > 32) {
            $this->addError($attribute, '银行过长[1-32位]');
            return;
        }
    }

    public function getUserWithdrawAccount(): ?UserWithdrawAccount
    {
        if (is_null($this->_userWithdrawAccount)) {
            $this->_userWithdrawAccount = UserWithdrawAccount::findOne(Yii::$app->user->identity->user->withdraw_id);
        }
        return $this->_userWithdrawAccount;
    }
}